<template>
	<view>
		<view class="item" v-for="(withdrawal,index) in affiliateWithdrawal" :key="index">
			<view>提现金额：{{ withdrawal.amount }}</view>
			<view>账户类型：{{ _account_type(withdrawal.accountType) }}</view>
			<view>状态：{{ _state(withdrawal.state) }}</view>
			<view v-if="withdrawal.state === '3'">拒绝原因：{{ withdrawal.auditRemark }}</view>
			<button v-if="withdrawal.state === '0'" class="cu-btn sub-btn bg-gradual-red margin-right"
				@click="_doDelete(withdrawal.awId, withdrawal.auId)">
				删除
			</button>
		</view>

		<view class="addSite bg-blue" v-if="!hasPendingOrProcessingWithdrawal"
			@tap="jump('/pages/affiliate/affiliateWithdrawal')">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>申请提现
			</view>
		</view>

	</view>
</template>

<script>
	import {
		isNull
	} from '@/lib/java110/utils/StringUtil.js';
	import {
		deleteAffiliateWithdrawal,
		queryAffiliateUser,
		queryAffiliateWithdrawal
	} from "../../api/affiliate/affiliateApi";

	export default {
		data() {
			return {
				apply: false,
				affiliateWithdrawal: null,
				amount: 0,
				accountType: 2,
				accountOptions: [{
						value: 1,
						label: '支付宝'
					},
					{
						value: 2,
						label: '微信'
					},
					{
						value: 3,
						label: '银行卡'
					}
				],
				affiliateUser: {
					totalCommission: 0,
					availableCommission: 0,
					withdrawnCommission: 0,
				},
			}
		},
		computed: {
			hasPendingOrProcessingWithdrawal() {
				return this.affiliateWithdrawal &&
					this.affiliateWithdrawal.length > 0 &&
					this.affiliateWithdrawal.some(item => item.state === "0" || item.state === "1");
			}
		},
		async onLoad() {
			await this._getAffiliateUser();
			await this.queryAffiliateWithdrawal();
		},
		onShow() {
			this._getAffiliateUser();
			this.queryAffiliateWithdrawal();
		},
		methods: {
			async _getAffiliateUser() {
				let param = {
					page: 1,
					row: 10,
					addType: 1
				};
				try {
					const data = await queryAffiliateUser(param);
					this.affiliateUser = data.data[0];
				} catch (error) {
					console.error('获取分销用户数据出错:', error);
				}
			},

			async queryAffiliateWithdrawal() {
				let param = {
					page: 1,
					row: 20
				};
				try {
					const data = await queryAffiliateWithdrawal(param);
					this.affiliateWithdrawal = data.data || [];
				} catch (error) {
					console.error('获取分销用户数据出错:', error);
				}
			},

			_doDelete: function(awId, auId) {
				deleteAffiliateWithdrawal({
						awId: awId,
						auId: auId
					}).then(_data => {
						uni.showToast({
							icon: 'none',
							title: _data.msg
						});
						if (_data.code == 0) {
							uni.navigateBack({
								delta: 1
							});
							return;
						}
					})
					.catch(error => {
						uni.showToast({
							icon: 'none',
							title: error
						});
					})

			},


			jump(path) {
				// 校验可提现佣金是否达到最低标准
				const available = parseFloat(this.affiliateUser.availableCommission);
				if (available < 100) {
					uni.showToast({
						icon: 'none',
						title: '可提现佣金不足100元，无法申请提现'
					});
					return;
				}
				this.vc.navigateTo({
					url: path
				})
			},

			_state: function(_state) {
				if (_state == "0") {
					return "申请中"
				}
				if (_state == "1") {
					return "处理中"
				}
				if (_state == "2") {
					return "已完成"
				}
				if (_state == "3") {
					return "已拒绝"
				}
			},
			_account_type: function(__account_type) {
				if (__account_type == "1") {
					return "支付宝"
				}
				if (__account_type == "2") {
					return "微信"
				}
				if (__account_type == "3") {
					return "银行卡"
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		margin: 20upx;
		border-radius: 10upx;
		padding: 30rpx 20rpx;
		background-color: white;

		view {
			padding: 10rpx;
			color: #666;
		}
	}

	.addSite {
		display: flex;
		justify-content: space-around;
		width: 600rpx;
		line-height: 100rpx;
		position: absolute;
		bottom: 30rpx;
		left: 80rpx;
		border-radius: 60rpx;
		font-size: 30rpx;

		.add {
			display: flex;
			align-items: center;
			color: #ffffff;

			.icon {
				margin-right: 10rpx;
			}
		}
	}
</style>